package com.galleryviewpager.m_qq;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;

import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class CurveView extends View {

    private Paint mPaint;
    private int mWidth, mHeight;

    private float mMoveCircleY = 200;
    private float mCircleY = 200, mCircleX = 300, mCircleRadius = 30, mSupX, mSupY, mMoveCircleRadius = mCircleRadius;

    private float lastY;
    private boolean isUp;

    private float mPaintStokeWidth = 3;

    private boolean isCanDraw = true;

    public CurveView(Context context) {
        this(context, null);
    }

    public CurveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CurveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(10f);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        mWidth = widthMode == MeasureSpec.EXACTLY ? MeasureSpec.getSize(widthMeasureSpec) : 500;

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        mHeight = heightMode == MeasureSpec.EXACTLY ? MeasureSpec.getSize(heightMeasureSpec) : 500;

        setMeasuredDimension(mWidth, mHeight);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(mPaintStokeWidth);

        mSupY = mCircleY + (mMoveCircleY - mCircleY) / 2;

        if (isCanDraw) {
            if ((mMoveCircleY - mCircleY) < mMoveCircleRadius * 3) {
                Path path = new Path();
                path.moveTo(mCircleX - mCircleRadius + mPaintStokeWidth / 2, mCircleY);
                path.quadTo(mCircleX, mSupY, mCircleX - mMoveCircleRadius + mPaintStokeWidth / 2, mMoveCircleY);
                path.lineTo(mCircleX + mMoveCircleRadius, mMoveCircleY);
                path.quadTo(mCircleX, mSupY, mCircleX + mCircleRadius, mCircleY);
                path.lineTo(mCircleX - mCircleRadius, mCircleY);
                path.close();
                canvas.drawPath(path, mPaint);

                if (isUp) {
                    canvas.drawCircle(mCircleX, mCircleY, mCircleRadius--, mPaint);
                    canvas.drawCircle(mCircleX, mMoveCircleY, mMoveCircleRadius, mPaint);
                } else {
                    canvas.drawCircle(mCircleX, mCircleY, mCircleRadius++, mPaint);
                    canvas.drawCircle(mCircleX, mMoveCircleY, mMoveCircleRadius, mPaint);
                }
            }
        } else {
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(mCircleX, mMoveCircleY, mMoveCircleRadius, mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                mMoveCircleY = event.getY();
                isUp = !(mMoveCircleY < lastY);
                lastY = mMoveCircleY;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                if ((mMoveCircleY - mCircleY) > mMoveCircleRadius * 6) {
                    isCanDraw = false;
                    invalidate();
                }
                break;
        }
        return true;
    }
}
